package dondeestamitransporte.actividadesChofer;



import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import dondeestamitransporte.actividades.R;
import dondeestamitransporte.actividadesPasajero.MenuInicioPasajeroActivity;
import dondeestamitransporte.actividadesPasajero.MenuPrincipal;
import dondeestamitransporte.mundo.Pasajeros;
import dondeestamitransporte.mundo.MundoChofer;
import dondeestamitransporte.mundo.RutaChofer;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;

public class MenuConductorActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_menu_conductor);
		ListView listaRutas = (ListView)findViewById(R.id.listaRutas);
		final MundoChofer mundo = MundoChofer.darInstancia();
		ArrayList<RutaChofer> rutas = mundo.rutas;
		
		List<Map<String, String>> data = new ArrayList<Map<String, String>>();
		if (rutas.size() == 0)
		{
			ArrayList<String> listItems=new ArrayList<String>();
			String shareFact = "No tienes rutas";
			ArrayAdapter<String> adapters=new ArrayAdapter<String>(this,
					android.R.layout.simple_list_item_1,
					listItems);
			listaRutas.setAdapter(adapters);
			listItems.add(shareFact);
			adapters.notifyDataSetChanged();
		}
		else
		{
			for (int i = 0; i < rutas.size(); i++) {
				RutaChofer ruta = rutas.get(i);
			    Map<String, String> datum = new HashMap<String, String>(2);
			    datum.put("title", ruta.nombre);
			    datum.put("info", "De " + ruta.direcciones.get(0)+ " a posicion " + ruta.direcciones.get(ruta.direcciones.size()-1));
			    data.add(datum);
			}
			
			final SimpleAdapter adapter = new SimpleAdapter(this, data,
			                                          android.R.layout.simple_list_item_2,
			                                          new String[] {"title", "info"},
			                                          new int[] {android.R.id.text1,
			                                                     android.R.id.text2});
			listaRutas.setAdapter(adapter);
			final Intent i = new Intent(this, VerRutaActivity.class);
			listaRutas.setOnItemClickListener(new OnItemClickListener() {
				@Override
			    public void onItemClick(AdapterView<?> parent, View view,
			        int position, long id) {
			        // When clicked, show a toast with the TextView text
			    	HashMap<String, Object> obj = (HashMap<String, Object>) adapter.getItem(position);
		            String nombreRuta = (String) obj.get("title");
		            String numero = nombreRuta.split(" ")[1];
		            i.putExtra("POSICION_RUTA", numero);
		            startActivity(i);
			    }
			});
		}
	}

	public boolean onCreateOptionsMenu(Menu menu) {

		// Inflate the menu; this adds items to the action bar if it is present.
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.menu_principal, menu);
		return true;
	}

	public boolean onOptionsItemSelected(MenuItem item)
    {
         
        switch (item.getItemId())
        {
        case R.id.Pasajero:
        	this.finish();
        	Intent launchNewIntent = new Intent(this,MenuInicioPasajeroActivity.class);
        	startActivity(launchNewIntent);
            return true;
 
        case R.id.Chofer:
        	this.finish();
        	Intent launchNewIntent2 = new Intent(this,MenuConductorActivity.class);
        	startActivity(launchNewIntent2);
            return true;
 
        default:
            return super.onOptionsItemSelected(item);
        }
    }  

	
	public void agregarRuta(View view)
	{
		Intent intent = new Intent(this, AgregarRutaPasoDirecciones.class);
		startActivity(intent);
	}
	
	
	public void darVistaConTabla()
	{
		MenuPrincipal parentTab = (MenuPrincipal) this.getParent();
        //parentTab.switchTabBar(1);
	}
	
	
	public void onRestart(){
	    super.onRestart();
	    Intent refresh = new Intent(this, MenuConductorActivity.class);
        startActivity(refresh);
        this.finish();
	}
	
	/**
	 * A placeholder fragment containing a simple view.
	 */
	/*public static class PlaceholderFragment extends Fragment {

		public PlaceholderFragment() {
		}

		@Override
		public View onCreateView(LayoutInflater inflater, ViewGroup container,
				Bundle savedInstanceState) {
			View rootView = inflater.inflate(R.layout.fragment_menu_conductor,
					container, false);
			return rootView;
		}
	}*/

}
